<?
    error_reporting(0);
    echo "<script language='JavaScript'>
    <!--
    var imgDir = 'images/';
    var preloadImages = new Array();
    preloadImages[0] = new Image();
    preloadImages[0].src = imgDir + 'spacer.gif';
    preloadImages[1] = new Image();
    preloadImages[1].src = imgDir + 'callout_a_1.gif';
    preloadImages[2] = new Image();
    preloadImages[2].src = imgDir + 'callout_a_2.gif';
    preloadImages[3] = new Image();
    preloadImages[3].src = imgDir + 'callout_a_3.gif';
    preloadImages[4] = new Image();
    preloadImages[4].src = imgDir + 'callout_a_4.gif';

    function confirm_del(what) {
		var is_confirmed=window.confirm('Are you sure you want to delete '+what+' plan?');
		return is_confirmed;
    }

    var myWin = '';
    function openWin(url, name, properties) {
	if (myWin && myWin.open && !myWin.closed) {
		myWin.close();
	}
	myWin = window.open(url, name, properties);
      	return false;
    }
    //-->
    </script>
    <script type='text/javascript' src='overlib.js'></script>
    <div id='overDiv' style='position:absolute; visibility:hidden; z-index:1000; background-color: none !important;'></div>
    <div align=left><b>";
    include('wallet_db.html');
    $interface = get_interface_settings($id);
    $current_template = $interface['template'];
    $current_template = sprintf("%01.0f",$current_template);
    $cur_template = $current_template + 1;
    if (isset($sub)) {
    	//------------------------------------------------------
		$sql['type']='SELECT';
		$sql['params']='name';
		$sql['table']='products';
		$sql['where']="product_id='$prod_id'";
		$sql['options']='';
		$sql['return']='struct';
	//------------------------------------------------------
	if (!$resnam=sql_exec($sql,$dbh81,'assoc')) {
		$errorMsg = translate('int_error');
		include_once('errorMsg.html');
		exit();
	}
	$plname = $resnam[0]['name'];
    }
	if (isset($sub) && $sub == 'edit') {
		echo translate('products_offer_edit',array('name' => $plname));
		echo "</div></b><br>";
		if (isset($step2_edit) && $step2_edit == 'yes') {
			include('actionOnPlans_edit_step2.html');
		} elseif (isset($step_2_completed) && $step_2_completed == 'yes') {
			include('actionOnPlans_edit_step3.html');
		} else {
			include('actionOnPlans_edit_step1.html');
		}
	} elseif (isset($sub) && $sub == 'del') {
		echo translate('products_offer_delete',array('name' => $plname));
		echo "</div></b><br>";
		include('actionOnPlans_del.html');
	} elseif (isset($move_pl)) {
		include('movePlan.html');
	} else {
	echo translate('products_offer');
    	echo "</div></b>";
        echo "<div align='left'>";

	unset($params);
	$params[sid] = $sid;
	$params[action] = "plansInfo";
	$plansurl = "$baseURL/?".encode_params($params);

	#if ($current_template == 15) {
		echo translate('over_table',array('url' => "http://www.duoservers.com/?r=$userName",
						'template_number' => "$cur_template",
						'plurl' => $plansurl));
	#} else {
	#	echo translate('over_table_1',array('url' => "http://www.duoservers.com/?r=$userName",
	#					'template_number' => "$cur_template",
	#					'plurl' => $plansurl));
	#}
    echo "</div>";
	if (isset($_POST['form_sent']) && $_POST['form_sent'] == 'yes') {
		foreach ($_POST as $key => $value) {
			if (preg_match("/^ploffer_.*/",$key)) {
				$store_offered_plans[] = $value;
			} elseif (preg_match("/^offer_.*/",$key)) {
				$template_offered_plans[] = $value;
			} elseif (preg_match("/^frdomoffer_.*/",$key)) {
				$free_domain_offering_plans[] = $value;
			} elseif (preg_match("/^planonindex_.*/",$key)) {
				$plans_on_index[] = $value;
			}
		}

		if (empty($store_offered_plans)) {
			$errorMsg =  translate('no_selected_plan');
			include_once('errorMsg.html');
		} elseif (empty($template_offered_plans)) {
			if ($current_template == 15) {
				$errorMsg =  translate('no_selected_tpl16_plan');
			} else {
				$errorMsg =  translate('no_selected_tpl1_plan');
			}
			include_once('errorMsg.html');
		} else {
			if (count($template_offered_plans) > 4) {
				$errorMsg =  translate('max_plans');
				include_once('errorMsg.html');
			} elseif (empty($plans_on_index)) {
				$errorMsg =  translate('no_plan_index');
				include_once('errorMsg.html');
			} elseif (count($plans_on_index) > 2) {
				$errorMsg = translate('exceeded_2plans');
				include_once('errorMsg.html');
			} else {
				//------------------------------------------------------
				$sql['type']='SELECT';
				$sql['params']='products';
				$sql['table']='stores';
				$sql['where']="store_id='".$_POST['s']."' and reseller_id='$id'";
				$sql['options']='';
				$sql['return']='struct';
				//------------------------------------------------------
				if (!$result1=sql_exec($sql,$dbh81,'assoc')) {
					$errorMsg = translate('int_error');
					include_once('errorMsg.html');
					exit();
				}
				$products=$result1[0]['products'];
				//------------------------------------------------------
				$sql['type']='SELECT';
				$sql['params']='product_id,offer_product';
				$sql['table']='products';
				$sql['where']="reseller_id='$id' AND type='plan'";
				$sql['options']='';
				$sql['return']='struct';
				//------------------------------------------------------
				if (!$res=sql_exec($sql,$dbh81,'assoc')) {
					$errorMsg = translate('int_error');
					include_once('errorMsg.html');
					exit();
				}
				foreach ($res as $val) {
					$present_offered_products[$val['product_id']] = $val['offer_product'];
				}
				foreach ($store_offered_plans as $vl) {
					$pl_offering[$vl] = 'on';
					$pl_periods[$vl][12] = 'on'; //!!!!
				}
				unset($vl);
				foreach ($free_domain_offering_plans as $vl) {
					if (in_array($vl,$store_offered_plans)) {
						$dom_free[$vl][12] = 'on';
					}
				}
				unset($vl);
				foreach ($plans_on_index as $vl) {
					if (in_array($vl,$store_offered_plans) && in_array($vl,$template_offered_plans)) {
						$pl_on_index[$vl] = 'on';
					}
				}
				unset($vl);
				foreach ($present_offered_products as $ke => $vl) {
					if (in_array($ke,$store_offered_plans)) {
						if (in_array($ke,$template_offered_plans)) {
							$sql = "UPDATE products SET offer_product='t' WHERE product_id='$ke'";
							if (!$res=pg_query($sql)) {
								$errorMsg = translate('int_error');
								include_once('errorMsg.html');
								exit();
							}
						} elseif (!in_array($ke,$template_offered_services) && $vl == 't') {
							$sql = "UPDATE products SET offer_product='f' WHERE product_id='$ke'";
							if (!$res=pg_query($sql)) {
								$errorMsg = translate('int_error');
								include_once('errorMsg.html');
								exit();
							}
						}
					} else {
						$sql = "UPDATE products SET offer_product='f' WHERE product_id='$ke'";
						if (!$res=pg_query($sql)) {
							$errorMsg = translate('int_error');
							include_once('errorMsg.html');
							exit();
						}
					}
				}
				$products['plans_offering'] = $pl_offering;
				$products['plans_on_index'] = $pl_on_index;
				$products['periods'] = $pl_periods;
				$products['domains_free'] = $dom_free;
				#exit(print_r($products));
				$products=base64_encode(serialize($products));
				$sql="UPDATE stores SET products='$products' WHERE store_id='$_POST[s]' and reseller_id='$id'";
					if (!$res=pg_query($sql)) {
						$errorMsg = translate('int_error');
						include_once('errorMsg.html');
						exit();
				} else {
					$okMsg = translate('success_change');
					include_once('okMsg.html');
				}
			}
		}
	}
	}
	if ($hide_rest != 'yes') {
	unset($sql);
	//------------------------------------------------------
        $sql['type']='SELECT';
        $sql['params']='min(seq) as max, max(seq) as min';
        $sql['table']='products';
        $sql['where']="reseller_id='$id' AND type='plan'";
        $sql['options']='';
        $sql['return']='struct';
        //------------------------------------------------------
        if (!$arres=sql_exec($sql,$dbh81,'assoc')) {
//                exit("<br><div class=errormsg>$dberror</div><br>");
                                        $errorMsg = translate('int_error');
                                        include_once('errorMsg.html');
                                        exit;
        }
	$min_seq=$arres[0][min];
	$max_seq=$arres[0][max];
	$select_products=translate('select_products');
	$offer_plan=translate('offer_plan');
	$plan_active=translate('plan_active');
	$products_list=translate('products_list');
	$free_domain=translate('hosting_free_domain');
	$plan_on_index=translate('products_index');
	$save_settings=translate('save_settings');
	echo "<script language='JavaScript'>
	<!--
	function change_props(maincheck,secondcheck,thirdcheck,fourthcheck) {
		if (maincheck.checked==false) {
			secondcheck.checked=false;
			secondcheck.disabled=true;
			thirdcheck.checked=false;
			thirdcheck.disabled=true;
			fourthcheck.checked=false;
			fourthcheck.disabled=true;
		} else {
			secondcheck.disabled=false;
			thirdcheck.disabled=false;
			fourthcheck.disabled=false;
		}
	}
	//-->
	</script>";
	//------------------------------------------------------
	$sql['type']='SELECT';
	$sql['params']='product_id, name, store_id, seq, offer_product, services, price, wholesale, plan_id';
	$sql['table']='products';
	$sql['where']="reseller_id='$id' AND type='plan'";
	$sql['options']='order by seq asc';
	$sql['return']='struct';
	#$sql['return']='query';
	//------------------------------------------------------
	if (!$result=sql_exec($sql,$dbh81,'assoc')) {
	    $errorMsg = translate('int_error');
		include_once('errorMsg.html');
		exit();
	}
	$store_id=$result[0][store_id]; // vzema samo pyrvia sreshtnat store!!!!
	//------------------------------------------------------
		$sql['type']='SELECT';
		$sql['params']='count(product_id) as count';
		$sql['table']='products';
		$sql['where']="reseller_id='$id' and type='plan'";
		$sql['options']='';
		$sql['return']='struct';
	//------------------------------------------------------

	if (!$relt=sql_exec($sql,$dbh81,'assoc')) {
                $errorMsg = translate('int_error');
                include_once('errorMsg.html');
                exit();
	}
	$count=$relt[0][count];
	if ($count>=20) {
                $errorMsg = translate('nomoreplans',array('count' => $count));
                include_once('errorMsg.html');
                echo "<br><br>";
	} else {
		$params[sid] = $sid;
		$params[action] = "createPlan";
		$createurl = "$baseURL/?".encode_params($params);
		unset($params);
		$new_count = 20 - $count;
		$moreplans=translate('moreplans',array('count' => "$count",'new_count' => "$new_count",'url' => "$createurl"));
		echo "<div align='left'>$moreplans</div><br>";
	}
	//------------------------------------------------------
        $sql['type']='SELECT';
        $sql['params']='products';
        $sql['table']='stores';
        $sql['where']="store_id='$store_id'";
        $sql['options']='';
        $sql['return']='struct';
        //------------------------------------------------------
	if (!$result1=sql_exec($sql,$dbh81,'assoc')) {
                 $errorMsg = translate('int_error');
                 include_once('errorMsg.html');
                 exit();
	}
	$products=$result1[0][products];
	#print_r($products);
	#exit();
	$params[sid] = $sid;
	$params[action] = "managePlans";
	$url = "$baseURL/?".encode_params($params);

	$hlp = translate('help');
	unset($params);
	$params[sid] = $sid;
	$params[action] = "managePlansHelp";
	$hlpurl = "$baseURL/?".encode_params($params);
	echo "<form name=plan_form action='$url' method='POST'>
	<TABLE width=550 class='bTable' cellspacing=0>
	<tr>
		<th colspan='5' align='left'>$select_products</th>
		<th colspan='3' align='right'><a href='#' onClick=\"return openWin('$hlpurl','Help','width=650, height=650, scrollbars=yes');\">$hlp</a></th>
	</tr>
	<tr>
		<th class='text'>$products_list</th>
		<th class='text'>$plan_active</th>
		<th class='text'>$offer_plan</th>
		<th class='text'>$plan_on_index</th>
		<th class='text'>$free_domain</th>
		<th class='text' colspan=3>".translate('actions')."</th>
	</tr>";
	//------------------------------------------------------
        $sql['type']='SELECT';
        $sql['params']='pref_value';
        $sql['table']='plan_builder_prefs';
        $sql['where']="pref_key='plan_min_price'";
        $sql['options']='';
        $sql['return']='struct';
        //------------------------------------------------------
	if (!$rst1=sql_exec($sql,$dbh81,'assoc')) {
                 $errorMsg = translate('int_error');
                 include_once('errorMsg.html');
                 exit();
	}
	$min_plan_price = sprintf("%01.2f",$rst1[0]['pref_value']*12);
	$overlib_plan_offer = translate('overlib_plan_offer');
	$overlib_caption_attention = translate('overlib_caption_attention');
	$overlib_plan_index = translate('overlib_plan_index');
	$overlib_plan_frdom = translate('overlib_plan_frdom');
	$disabled_checkbox = translate('disabled_checkbox');
	$disabled_checkbox2 = translate('disabled_checkbox2');
	$plpl = translate('plan');

	foreach ($result as $value) {
	    //------------------------------------------------------
		$sql['type']='SELECT';
		$sql['params']='count(distinct pu.client_id) as count';
		$sql['table']='purchases as pu, products as pr, clients as c';
		$sql['where']="pr.product_id='".$value['product_id']."' and pr.product_id=pu.product_id and pu.client_id=c.client_id and c.blocked=0";
		$sql['options']='group by pu.product_id';
		$sql['return']='struct';
		//------------------------------------------------------
		if (!$result=sql_exec($sql,$dbh81,'assoc')) {
       			$count = '0';
		} else {
			$count = $result[0]['count'];
		}
		unset($params);
		$params['sid'] = $sid;
		$params['action'] = 'managePlans';
		$params['sub'] = 'del';
		$params['prod_id'] = $value['product_id'];
		$params['pl_name'] = $value['name'];
		$params['cl_count'] = $count;
		$delurl="$baseURL/?".encode_params($params);
		######
		unset($params);
		$params['sid'] = $sid;
		$params['action'] = 'managePlans';
		$params['sub'] = 'edit';
		if ($value['plan_id'] == 'p') {
			$params['prebuild'] = 'yes';
		} else {
			$params['prebuild'] = 'no';
		}
		$params['prod_id'] = $value['product_id'];
		$params['cl_count'] = $count;
		$editurl="$baseURL/?".encode_params($params);
		######
		unset($params);
		$params[sid]=$sid;
		$params[action]='managePlans';
		$params[prod_id]=$value[product_id];
		$params[pl_min_seq]=$min_seq;
		$params[pl_max_seq]=$max_seq;
		if ($value[seq]>$max_seq && $value[seq]<$min_seq) {
			$params[pl_cur_seq]="$value[seq]";
			$params[move_pl]='up';
			$movurl="$baseURL/?".encode_params($params);
			$uparrow="<a href='$movurl'><img src='images/arrow_red_up.gif' border=0></a>";
			$params[move_pl]='down';
			$movurl="$baseURL/?".encode_params($params);
			$downarrow="<a href='$movurl'><img src='images/arrow_red_down.gif' border=0></a>";
		} elseif ($value[seq]==$min_seq) {
			$params[pl_cur_seq]="$value[seq]";
			$params[move_pl]='up';
			$movurl="$baseURL/?".encode_params($params);
			$uparrow="<a href='$movurl'><img src='images/arrow_red_up.gif' border=0></a>";
			$downarrow="";
		} elseif ($value[seq]==$max_seq) {
			$params[pl_cur_seq]="$value[seq]";
			$params[move_pl]='down';
			$movurl="$baseURL/?".encode_params($params);
			$uparrow="";
			$downarrow="<a href='$movurl'><img src='images/arrow_red_down.gif' border=0></a>";
		}
		$query="SELECT * FROM purchases WHERE product_id='$value[product_id]'";
		$res=pg_query($dbh81,$query);
		if (!$res) {
			$errorMsg = translate('int_error');
			include_once('errorMsg.html');
			exit();
		}
		$num=pg_num_rows($res);
		if ($num!=0 && !in_array($value['name'],array('Business','Corporate'))) {
			$pos_edit="<a href=\"$editurl\" $dis>edit</a>";
			$pos_del="<a href=\"$delurl\">delete</a>";
		} elseif ($value['plan_id'] == 'p') {
			$pos_edit="<a href=\"$editurl\" $dis>edit</a>";
			$pos_del="<a href=\"$delurl\">delete</a>";
		} elseif (in_array($value['name'],array('Business','Corporate'))) {
			$pos_edit='-';
			$pos_del='-';
		} else {
			$pos_edit="<a href=\"$editurl\" $dis>edit</a>";
			$pos_del="<a href=\"$delurl\" onClick=\"return confirm_del('$value[name]');\" $dis>delete</a>";
		}
		if ($products[plans_offering][$value[product_id]]=='on') {
			$ch='checked';
		} else {
			$ch='';
		}
		if (is_array($products[domains_free][$value[product_id]]) && in_array('on',$products[domains_free][$value[product_id]])) {
			$ch1='checked';
		} else {
			$ch1='';
		}
		if ($products[plans_on_index][$value[product_id]]=='on') {
			$ch2='checked';
		} else {
			$ch2='';
		}
		if ($value['offer_product'] == 't') {
			$chp='checked';
		} else {
			$chp='';
		}

		unset($params);
		$params['sid'] = $sid;
		$params['action'] = "showPlanDetails";
		$params['pid'] = $value['product_id'];
		$params['rid'] = $id;
		$showurl = "$baseURL/?".encode_params($params);
		$d_space = sprintf("%01.0f",$value['services']['disk_space']/1024);
		$traff = sprintf("%01.0f",$value['services']['traffic']/1024/1000);
		$wpmon = sprintf("%01.2f",$value['wholesale'][12]['USD']/12);
		$rpmon = sprintf("%01.2f",$value['price'][12]['USD']/12);
		if ($value['services']['domain'] == 1) {
			$dom = $value['services']['domain']." domain";
		} else {
			$dom = $value['services']['domain']." domains";
		}

		if (in_array($value['plan_id'],array('p','b','c'))) {
			if ($value['plan_id'] == 'b') {
				$pl_tmp = translate('pl_tmp_business');
			} elseif ($value['plan_id'] == 'c') {
				$pl_tmp = translate('pl_tmp_corporate');
			} else {
				$pl_tmp = translate('pl_tmp_pre');
			}
		} else {
			$pl_tmp = translate('pl_tmp_cust');
		}
		$pl_info = translate('pl_info',array('d_space' => $d_space,
							'wpmon' => $wpmon,
							'traff' => $traff,
							'rpmon' => $rpmon,
							'dom' => $dom,
							'count' => $count));
		$pl_caption = translate('pl_caption',array('name' => $value['name'],
							'pl_tmp' => $pl_tmp));
		echo "<tr>
			<td align='left'><a href='#' onmouseover='overlib(\"$pl_info\", CAPTION, \"$pl_caption\", VAUTO, HAUTO);' onmouseout='return nd();' onClick=\"return openWin('$showurl','PlanDetails','width=650, height=650, scrollbars=yes');\">";
		if (in_array($value['plan_id'],array("p","b","c"))) {
			echo "<span style='color: #0E8003; font-weight: bold;'>".$value['name']."</span>";
		} else {
			echo "<span style='color: #9900B0; font-weight: bold;'>".$value['name']."</span>";
		}
		echo "</a></td>
			<td align='center'><input type=checkbox name='ploffer_$value[product_id]'
				value='$value[product_id]' $ch
				onClick=\"change_props(document.plan_form.ploffer_$value[product_id],document.plan_form.frdomoffer_$value[product_id],document.plan_form.planonindex_$value[product_id],document.plan_form.offer_$value[product_id]);\"></td>
			<td align='center'>";
		#if ($current_template == 15 or in_array($value['plan_id'],array('b','c'))) {
		if (($value['wholesale'][12]['USD'] < $min_plan_price) or ($d_space < 1000) or ($traff < 10)) {
				echo "<a href='#' onmouseover='overlib(\"".$disabled_checkbox2."\", VAUTO, HAUTO);' onmouseout='return nd();' onClick='return false;'><img src='images/ico_plan_offer.gif' border='0'></a>";
			} else {
				echo "<input type='checkbox' name='offer_".$value['product_id']."' value='".$value['product_id']."' $chp $disa>";
			}
		#} else {
		#	echo "<a href='#' onmouseover='overlib(\"".$disabled_checkbox1."\", VAUTO, HAUTO);' onmouseout='return nd();' onClick='return false;'><img src='images/ico_disabled_checkbox.gif' border='0'></a>";
		#}

		echo "</td>";
		echo "<td align='center'>";
		#if ($current_template == 15 or in_array($value['plan_id'],array('b','c'))) {
		if (($value['wholesale'][12]['USD'] < $min_plan_price) or ($d_space < 1000) or ($traff < 10)) {
				echo "<a href='#' onmouseover='overlib(\"".$disabled_checkbox2."\", VAUTO, HAUTO);' onmouseout='return nd();' onClick='return false;'><img src='images/ico_plan_offer.gif' border='0'></a>";
			} else {
				echo "<input type=checkbox name='planonindex_".$value['product_id']."' value='".$value['product_id']."' $ch2 $disa>";
			}
		#} else {
		#	echo "<a href='#' onmouseover='overlib(\"".$disabled_checkbox1."\", VAUTO, HAUTO);' onmouseout='return nd();' onClick='return false;'><img src='images/ico_disabled_checkbox.gif' border='0'></a>";
		#}
		echo "</td>";
		echo "<td align='center'>";
		#if ($current_template == 15 or in_array($value['plan_id'],array('b','c'))) {
			if (($value['wholesale'][12]['USD'] < $min_plan_price) or ($d_space < 1000) or ($traff < 10)) {
				echo "<a href='#' onmouseover='overlib(\"".$disabled_checkbox2."\", VAUTO, HAUTO);' onmouseout='return nd();' onClick='return false;'><img src='images/ico_plan_offer.gif' border='0'></a>";
			} else {
				echo "<input type=checkbox name='frdomoffer_$value[product_id]' value='$value[product_id]' $ch1></td>";
			}
		#} else {
		#		echo "<a href='#' onmouseover='overlib(\"".$disabled_checkbox1."\", VAUTO, HAUTO);' onmouseout='return nd();' onClick='return false;'><img src='images/ico_disabled_checkbox.gif' border='0'></a>";
		#}
		echo "</td><td align='center'>$pos_edit</td>
			<td align='center'>$pos_del</td>
			<td align=center>$uparrow&nbsp;$downarrow</td>
			</tr>";
	}
	echo "<tr>
	<td colspan=8 align=center><input type=submit name=submit value='$save_settings'>
		<input type=hidden name=form_sent value='yes'>
		<input type=hidden name=s value='$store_id'></td>
	</tr>";
	echo "</table>
	</form>";
	#echo "<div align='left'>".translate('legend',array('url' => "http://www.duoservers.com/?r=$userName"))."</div>";
}

?>
